//
//  CZHomeTableViewController.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/8.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit

class CZHomeTableViewController: CZBaseTableViewController {

    
    var status: [CZStatusModel]?
    override func viewDidLoad() {
        super.viewDidLoad()
        // 用户没有登录不需要做一下操作
        if !CZUseraccountViewModel.sharInatance.userLogin {
            HMPrint("没有登录不做一下操作")
            return
        }

      setNavBtn()

        CZStatusViewModel.sharedInstance.loadStatus { (statuses, error) in
            if error != nil{
            
            HMPrint("数据有问题\(error)")
                return
            }
            //拿到数据
            self.status = statuses
            HMPrint(self.status)
            self.tableView.reloadData()
            //注册
            self.tableView.registerClass(CZStatusCell.self, forCellReuseIdentifier: "cell")
//            self.tableView.registerClass(UICollectionViewCell.self, forCellReuseIdentifier: "cell")

            
            //自定布局的三个步骤
            self.tableView.rowHeight = UITableViewAutomaticDimension
            self.tableView.estimatedRowHeight = 200
            self.tableView.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
            self.tableView.separatorStyle = .None
            
        }
    }

    private func setNavBtn(){

        self.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: #selector(CZHomeTableViewController.personBtnDidClick))
    
    
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(CZHomeTableViewController.QRcodeBtnDidClick))
    
        let name = CZUseraccountViewModel.sharInatance.userAccount?.screen_name ?? "好霸气的名字"
        let navbutton = CZHomButtonItem(title: name)
        navbutton.addTarget(self, action: #selector(CZHomeTableViewController.titleBtnDidClick(_:)), forControlEvents: .TouchUpInside)
        self.navigationItem.titleView = navbutton
}

    @objc func titleBtnDidClick(button: UIButton){
    
        HMPrint("title按钮点击了")
        button.selected = !button.selected
    
        UIView.animateWithDuration(HMDefautlAnimDuration) { 

            //就近原则 将angle值减去一点点就可以实现旋转方向改变
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01)) : CGAffineTransformIdentity
        }
    }
    
    @objc func personBtnDidClick(){
    HMPrint("头像按钮点击了")
    
    }
    @objc  func QRcodeBtnDidClick(){

    HMPrint("扫描按钮点击了")


    }
   // MARK: - ******************* 数据源方法 ********************
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.status?.count ?? 0
        
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //这样写可以重用
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CZStatusCell
//        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

//        let cell = CZStatusCell(style: .Default, reuseIdentifier: "cell")
        
//        cell.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
        
        let status = self.status![indexPath.row]
//
//        cell.textLabel?.text = status.text
        cell.status = status
        
        
        return cell
    }
    
    
    /// cell不要高亮
    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }

    
    
    
    
    
    
    
    }
